package com.atguigu.serviceucenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.serviceucenter.entity.LoginVo;
import com.atguigu.serviceucenter.entity.RegisterVo;
import com.atguigu.serviceucenter.entity.UcenterMember;
import com.atguigu.serviceucenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author JY
 * @since 2021-01-29
 */
@RestController
@RequestMapping("/api/ucenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;


    //用户登录
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo){
        //返回token值
        String token = memberService.login(loginVo);
        return R.ok().data("token",token);
    }

    //用户注册
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }

    //根据token获取用户信息
    @GetMapping("getLoginInfo")
    public R getLoginInfo(HttpServletRequest request){
        //调用jwt工具类，根据request对象头信息获取用户信息
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("item",member);
    }

    //根据id获取用户信息
    @GetMapping("getInfoById/{id}")
    public com.atguigu.commonutils.vo.UcenterMember getInfoById(@PathVariable String id){
        UcenterMember ucenterMember = memberService.getById(id);
        com.atguigu.commonutils.vo.UcenterMember member = new com.atguigu.commonutils.vo.UcenterMember();
        BeanUtils.copyProperties(ucenterMember,member);
        return member;
    }

    //统计某一天的注册人数
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day){
        Integer count = memberService.countRegister(day);
        return R.ok().data("countRegister", count);
    }
}

